﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DotSpatial.Controls;

using System.IO;
using DotSpatial.Data;

namespace HAGISV2
{
    public class GisManager
    {
        public GisManager()
        {
            _interactiveSystem = new InteractiveSystem(this);

            _layerSystem = new LayerSystem(this);

            _assistSystem = new AssistSystem(this);
 
        }


        #region 内部管理工具部分

        InteractiveSystem _interactiveSystem;

        public InteractiveSystem InteractiveSystem
        {
            get { return _interactiveSystem; }
           
        }

        LayerSystem _layerSystem;

        public LayerSystem LayerSystem
        {
            get { return _layerSystem; }

        }


        AssistSystem _assistSystem;

        public AssistSystem AssistSystem
        {
            get { return _assistSystem; }
          
        }

        #endregion


        #region 公共属性部分
         public FMAPVIEW VIEWMODE
       {
           get { return this._layerSystem.FMapView; }

           set { this._layerSystem.FMapView = value; }
       }
   

        #endregion

        #region 关键部件配置部分

        Map _currentMap;

        public Map CurrentMap
        {
            get { return _currentMap; }
            set { 
                _currentMap = value;

                this.OnSetMap();
            
            }
        }

        protected void OnSetMap()
        {
            this._currentMap.ProjectionModeDefine = ActionMode.Never;
 
        }

        #endregion

        #region 对外公共函数

        #region 项目管理部分

        string _projName = "main.gis";

        string _mapXml = "main.map.xml";

        string _projAssistName = "main.ass";

        /// <summary>
        /// 实际的工作目录
        /// </summary>
        string _workPath;


        



        /// <summary>
        /// 程序主保存文件的地址和文件名
        /// </summary>
        public string AbsProjPathName
        {
            get { return this._workPath + _projName; }

        }


        /// <summary>
        /// xml相关的保存地址名称
        /// </summary>
        public string AbsMapPathName
        {
            get { return this._workPath + _mapXml; }
        }


        public string AbsProjAssistName
        {
            get { return this._workPath + this._projAssistName; }
        }


        /// <summary>
        /// 工作目录，形式为c:\test\projpath\gis
        /// </summary>
        public string workPath
        {
            get { return this._workPath; }


        }

        public string layerPath
        {
            get
            {
                return this._workPath + "layers\\";
            }
        }



        protected void ResetGisState()
        {
            this.CurrentMap.FunctionMode = FunctionMode.None;
            this.CurrentMap.FunctionMode = FunctionMode.Pan;
            this._interactiveSystem.CurrentLayer = null;
        }

        public bool SaveProj()
        {
             

            this._assistSystem.SaveProj();

            return this._layerSystem.SaveProj();
        }

        public bool OpenProj(string wPath)
        {

            this._workPath = wPath + "gis\\";

            bool b = false;

            try
            {

                this.AssistSystem.OpenProj();
                b = _layerSystem.OpenProj();

                //Start与左侧树形显示同步
                this._currentMap.Layers.SuspendEvents();

                foreach (var item in this._layerSystem.LayerCollection)
                {
                    item.Visible = true;
                }
                this._currentMap.Layers.ResumeEvents();

                this.ZoomToExtent();
                //End

                ////重置对应的标志位

                //this.ResetGisState();
               



            }
            catch (Exception e)
            {
                Console.WriteLine("打开gis时发生错误！" + e.Message);

                return false;

            }

            return b;

        }


        /// <summary>
        /// 打开一个工作路径
        /// </summary>
        /// <param name="wPath">路径地址，形式为c;\proj\</param>
        /// <returns></returns>
        public bool NewProj(string wPath)
        {

            this._workPath = wPath + "gis\\";

            if (!Directory.Exists(this._workPath))
            {

                Directory.CreateDirectory(this._workPath);

            }
            else
            {


                ///如果当前存在目录，并且目录不为空，需要提示用户是否继续
                ///但是由于这个是组件程序，询问的功能是主程要做的，我总是混合两者的概念，所以这里不需要问了，直接覆盖               

                System.IO.File.SetAttributes(this._workPath, FileAttributes.Normal);
                Directory.Delete(this._workPath, true);

                Directory.CreateDirectory(this._workPath);

            }


            if (!Directory.Exists(this.layerPath))
            {
                Directory.CreateDirectory(layerPath);

            }

            this._layerSystem.NewProj();
            this._assistSystem.NewProj();



            //this.ResetGisState();

            return this.SaveProj();


        }

        public bool CloseProj()
        {
            this.ResetGisState();
            this._workPath = "";

            this._assistSystem.CloseProj();

            return this._layerSystem.CloseProj();


        }


       



        #endregion


        /// <summary>
        /// 缩放到合适大小
        /// </summary>
        public void ZoomToExtent()
        {
            Extent extent = null, helpEx = null;

            foreach (Layer item in this._layerSystem.LayerCollection)
            {
                helpEx = item.GetLayerExtent();
                if (helpEx != null)
                    if (extent == null)
                        extent = helpEx;
                    else
                        extent.ExpandToInclude(helpEx);
            }

            if (extent != null)
                if (extent.Width == 0 || extent.Height == 0)
                {
                    extent.ExpandBy(100);
                    this._currentMap.ViewExtents = extent;
                }
                else
                    this._currentMap.ViewExtents = extent;
        }

        public void ZoomToCurrentLayer(string id)
        {
            Layer layer = this.LayerSystem.LayerCollection.GetLayer(id);

            if (layer == null)
                return;

            Extent extent = layer.GetLayerExtent();

            if (extent != null)
                if (extent.Width == 0 || extent.Height == 0)
                {
                    extent.ExpandBy(100);
                    this._currentMap.ViewExtents = extent;
                }
                else
                    this._currentMap.ViewExtents = extent;
        }


        public void SetMapMode(MAPMODE mode)
        {

            this._interactiveSystem.SetMapMode(mode);


        }


        #endregion

    }
}
